var hxsd_tools = {};
hxsd_tools.modal = function(){
	var modal = $("<div>");
	modal.css({
		width:"100%",
		height:"100%",
		background: "#000",
		opacity: 0.3,
		"z-index": 50,
		position: "fixed",
		top: 0,
		left: 0
	});
	$('body').append(modal);
	return function(){
		modal.remove();
	}
}

//jquery 居中
hxsd_tools.setCenter = function(obj){
	function center () {
		let l = ($(window).width() - obj.outerWidth())/2;
		let t = ($(window).height() - obj.outerHeight())/2;
		obj.css({
			'position':'absolute',
			'left': l,
			'top': t
		});
	}
	center();
	$(window).resize(center);
	
}

//drag拖动
hxsd_tools.drag = function(obj, title) {
	title = title || obj;
	title.mousedown(function(ev){
		ev.preventDefault();
		var disX = ev.pageX - obj.position().left;
		var disY = ev.pageY - obj.position().top;
		$(document).mousemove(function(ev){
			var l = ev.pageX - disX;
			var t = ev.pageY - disY;
			console.log(l,t);
			if(l<0)l=0;
			if(t<0)t=0;
			var maxLeft = $(window).width()-obj.outerWidth();
			var maxTop = $(window).height()-obj.outerHeight();
			if(l>maxLeft)l=maxLeft;
			if(t>maxTop)t=maxTop;
			obj.css({
				'left': l,
				'top': t
			});
		});
		$(document).mouseup(function(){
			$(document).off('mousemove');
		})
	});
}

hxsd_tools.alertBox = function(text) {
	var oAlertBox = $('<div class="alertBox"><h4 class="title">警告</h4><a class="closeBtn" href="javascript:;">×</a><div class="popCont"><p>'+text+'</p></div><div class="btnBar"><button type="button" class="okBtn">确定</button></div></div>');
	$("body").append(oAlertBox);
	$.setCenter(oAlertBox);
	var remove = $.modal();
	$(".alertBox .closeBtn,.alertBox .okBtn").click(function(){
		remove();
		oAlertBox.remove();
	})
}

hxsd_tools.confirm = function(text,fn) {
	var confirmBox = $('<div class="alertBox confrimBox"><h4 class="title">请确认</h4><a class="closeBtn" href="javascript:;">×</a><div class="popCont"><p>'+text+'</p></div> <div class="btnBar"><button type="button" class="okBtn">确定</button><button type="button" class="cancelBtn">取消</button></div></div>');
	$("body").append(confirmBox);
	$.setCenter(confirmBox);
	var remove =  $.modal();
	$(".confrimBox .closeBtn,.confrimBox .cancelBtn").click(function () {
		remove();
		confirmBox.remove();
	});
	$(".confrimBox .okBtn").click(function () {
		remove();
		confirmBox.remove();
		fn&&fn();
	});
}
$.extend(hxsd_tools);

//--------------------------jquery原型上扩展方法---------------------------------------

//jQuery.prototype.showCenter = function(){}
//$.fn.showCenter = function(){}
$.fn.extend({
	shake: function (set,fn){
		return this.each(function(){
			var def = {
				mode: "left",
				num: 20,
			}
			var _this = $(this);
			var opt = $.extend(def,set);
			var objPosition = (opt.mode == "left")? _this.position().left : _this.position().top;
			var arr = [];
			for(var i=opt.num;i>0; i-=2){
				arr.push(i);
				arr.push(-i);
			};
			arr.push(0);
			var arrL = arr.length;
			var n=0;
			clearInterval(_this.timer);
			_this.timer = setInterval(function(){
				_this.css(opt.mode,objPosition+arr[n] + "px");
				n++;
				if(n==arrL){
					clearInterval(_this.timer);
					n=0;
					fn&&fn();
				}
			},30);
		})
	},
	showCenter: function(){
		return this.each(function(){
			var _this = $(this);
			function center () {
				let l = ($(window).width() - _this.outerWidth())/2;
				let t = ($(window).height() - _this.outerHeight())/2;
				_this.css({
					'position':'absolute',
					'left': l,
					'top': t
				});
			}
			center();
			$(window).resize(center);
		})
	},
	drag: function(set) {
		var def = {
			title:'',
		};
		var opt = $.extend(def,set);
		return this.each(function(){
			var _this = $(this);
			title = opt.title || _this;
			if (opt.title) {	//找个每个对象里的title
				title = _this.children(opt.title);
			}
			title.mousedown(function(ev){
				ev.preventDefault();
				var disX = ev.pageX - _this.position().left;
				var disY = ev.pageY - _this.position().top;
				$(document).mousemove(function(ev){
					var l = ev.pageX - disX;
					var t = ev.pageY - disY;
//					console.log(l,t);
					if(l<0)l=0;
					if(t<0)t=0;
					var maxLeft = $(window).width()-_this.outerWidth();
					var maxTop = $(window).height()-_this.outerHeight();
					if(l>maxLeft)l=maxLeft;
					if(t>maxTop)t=maxTop;
					_this.css({
						'left': l,
						'top': t
					});
				});
				$(document).mouseup(function(){
					$(document).off('mousemove mouseup');
				})
			});
		})
	},
	modal: function(){
		return this.each(function(){
			var modal = $("<div>");
			modal.css({
				width:"100%",
				height:"100%",
				background: "#000",
				opacity: 0.3,
				"z-index": 50,
				position: "fixed",
				top: 0,
				left: 0
			});
			$('body').append(modal);
			return function(){
				modal.remove();
			}
		})
	},
})
